﻿using GuanYijia.Application.ProcurementManagement.ProcurementDTO;
using GuanYijia.DoMain;
using GuanYijia.DoMain.PageDto;
using GuanYijia.DoMain.RBAC;
using GuanYijia.Repository;
using GuanYiJia.WebApi;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Panda.DynamicWebApi;
using Panda.DynamicWebApi.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GuanYijia.Application.RequisitionManagement
{
    [DynamicWebApi]
    [ApiExplorerSettings(GroupName = "v1")]
    public class ProcurementDataService: IDynamicWebApi
    {
        private readonly IBaseRepository<ProductInfo> _productInfoRepository;
        private readonly IBaseRepository<Requisition> _requisitionRepository;
        private readonly IBaseRepository<User> _userRepository;
        private readonly IBaseRepository<UserBusiness> _userBusiness;


        public ProcurementDataService(IBaseRepository<ProductInfo> productInfoRepository, IBaseRepository<Requisition> requisitionRepository, IBaseRepository<User> userRepository, IBaseRepository<UserBusiness> userBusiness)
        {
            _productInfoRepository = productInfoRepository;
            _requisitionRepository = requisitionRepository;
            _userRepository = userRepository;
            _userBusiness = userBusiness;
        }
        /// <summary>
        /// 操作员下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [SkipCustomFilterAttribute]
        public object GetUserInfo()
        {

            var userQuery = _userRepository.GetQueryable(tracking: false);
            var userBusinessQuery = _userBusiness.GetQueryable(tracking: false);

            var query = from a in userQuery
                        join b in userBusinessQuery on a.userId equals b.userId
                        select new
                        {
                            a.userId,
                            a.username,
                            a.login_name,
                            a.password,
                            a.leader_flag,
                            a.position,
                            a.department,
                            a.email,
                            a.phonenum,
                            a.ismanager,
                            a.isystem,
                            a.Status,
                            a.description,
                            a.remark,
                            a.tenant_id,
                            b.roleid
                        };
            var result= query.ToList();
            return result;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [SkipCustomFilterAttribute]
        public async Task<PageResultDto<RequisitionOutPut>> GetRequisitionShow(RequisitionInPut input)
        {
            var list = new PageResultDto<RequisitionOutPut>() { TotalCount = 0, TotalPage = 0, Data = new List<RequisitionOutPut>() };
            var res = from a in _requisitionRepository.GetQueryable(tracking: false)
                      join b in _productInfoRepository.GetQueryable(tracking: false) on a.productid equals b.productid
                      join c in _userRepository.GetQueryable(tracking: false) on a.userId equals c.userId
                      select new RequisitionOutPut
                      {
                          requisitionid = a.requisitionid,
                          document_number = a.document_number,
                          product_name = b.product_name,
                          document_date = a.document_date,
                          userId = c.userId,
                          login_name = c.login_name,
                          quantity = a.quantity,
                          remark = a.remark,
                          status = a.status
                      };
            if (!string.IsNullOrEmpty(input.document_number))
            {
                res = res.Where(x => x.document_number == input.document_number);
            }
            if (!string.IsNullOrEmpty(input.product_name))
            {
                res = res.Where(x => x.product_name.Contains(input.product_name));
            }
            if (!string.IsNullOrEmpty(input.kdate))
            {
                res = res.Where(x => x.document_date >= DateTime.Parse(input.kdate));
            }
            if (!string.IsNullOrEmpty(input.edate))
            {
                res = res.Where(x => x.document_date < DateTime.Parse(input.edate));
            }
            if (input.userId != null)
            {
                 res = res.Where(x => x.userId == input.userId); 
            }
            if (input.status != null)
            {
                res = res.Where(x => x.status == input.status);
            }
            if (!string.IsNullOrEmpty(input.remark))
            {
                res = res.Where(x => x.remark == input.remark);
            }
            list.TotalCount = res.Count();
            list.TotalPage = (int)Math.Ceiling(list.TotalCount * 1.0 / input.pageSize);
            list.Data = await res.OrderByDescending(x => x.requisitionid).Skip((input.pageIndex - 1) * input.pageSize).Take(input.pageSize).ToListAsync();
            return list;
        }
        /// <summary>
        /// 逻辑单删
        /// </summary>
        /// <param name="requisitionid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<int> DeleteRequisition(int requisitionid)
        {
            var requisition = await _requisitionRepository.GetAsync(requisitionid);
            requisition.IsDel = true;
            return await _requisitionRepository.UpdateAsync(requisition);
        }
        /// <summary>
        /// 逻辑批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> RangeDeleteRequisition(List<int> ids)
        {
            var requisitionList = await _requisitionRepository.GetAllAsyncList(x => ids.Contains(x.requisitionid));
            foreach (var item in requisitionList)
            {
                item.IsDel = true;
            }
            return await _requisitionRepository.UpdateRangeAsync(requisitionList);
        }
    }
}
